Online-Academy
Look, Read, Understand, Apply

Creating django project

Setting up an website usign django

  • First create a virtual environment:
    c:\django\>python -m venv <<name of venvironment>>

    Let's suppose name for our virtual environment is myenv then command will be

    python -m venv myenv

    Actually the command to create virtual environment will create a folder with the name of the virtual environment, here, myenv.

  • After creating virtual environment, go to Scripts folder (type dir command in windows)
    and type activate.bat command.

    This is activate virtual environment. After activating virtual environment come out of the Scripts folder, to parent folder of Scripts

  • Then install django (be in the virtual environment.
    pip install django
  • Then create a project: (should be in the virtual environment.

    django-admin startproject <<name of project >>

    Let's suppose name of project be my_first_site

    django-admin startproject my_first_site

    This is create a folder my_first_site, inside this folder (project folder) we will see folder with name my_first_site again, there will be file manage.py also.
    We will create application inside the my_first_site (project folder).

  • Create application (go to folder my_first_site (project folder), use cd command in windows)

    command to create application is :

    python manage.py startapp <<name_of_application>>

    First check if manage.py file is there in the folder or not, manage.py file is necessary to create an application. Let's suppose name of our application is restaurant then

    python manage.py startapp restaurant

  • Createing views:

    Go to the views folder inside your app, here restaurant, find their views.py file.

    Type following code,

    
    from django.shortcuts import render
    from django.http import HttpResponse
    
    # Create your views here.
    def showMessage(request):
        return HttpResponse("this is a message")
    

    The function defined showMessage will be use in the urls.py file

  • Create a urls.py file inside the restaurant folder, and write following code inside it.

    from django.urls import path
    from . import views
    
    urlpatterns = [
        path('showmessage/', views.showMessage, name='showmessage'),
    ]
    

    Here, urlpatterns is a list which specifies how to access the view in the browser window. the tuple: path specify url (showmessage), view to access (view.showMessage), name of view.
    We will write localhost:8000/showmessage in the address bar of browser.

  • Now we have to specify the urls of our site (here, restaurant) in the file urls.py of root directory (here, my_first_site(folder inside of project folder).

    copy following code to urls.py of the root directory

    from django.contrib import admin
    from django.urls import path,include
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path("",include("restaurant.urls")),
    ]
    
  • To run the django server use this command, be in the root folder( here my_first_site, in that folder you will find manage.py file. (Actually you will find my_first_site folder inside my_first_site, you have to be in the parent my_first_site folder to run command and command is

    python manage.py runsever